import UIKit

/* 枚举型 */
// 1
enum Direction {
    case East
    case South
    case West
    case North
}

func adjacentCity(direction: Direction) -> String {
    switch direction{
    case .East:
        return "East"
    case .South:
        return "South"
    case .West:
        return "West"
    case .North:
        return "North"
    }
}

let northNeighbour = adjacentCity(direction: Direction.North)
print(northNeighbour)

// 2
enum Direction2 : Int{
    case East = 0
    case South
    case West
    case North
}
let currentDirection : Direction2 = .North
print(currentDirection.rawValue)

// 3
enum Direction3 : String{
    case East = "East"
    case South = "South"
    case West = "West"
    case North = "North"
}

let currentDirection3 = Direction3.North
print(currentDirection3.rawValue)

// 4
enum OperationFeedback {
    case Done(Int)
    case Fail(String)
}

var balanceOfATM = 10000

func withdrawFromATM(wantMoney: Int) -> OperationFeedback {
    if wantMoney <= balanceOfATM {
        balanceOfATM -= wantMoney
        return OperationFeedback.Done(balanceOfATM)
    } else {
        return OperationFeedback.Fail("balanceOfATM is not enough")
    }
}

var getMoney : OperationFeedback = withdrawFromATM(wantMoney: 1288)
switch getMoney {
case .Done(let nowBalanceOfATM) :
    print("\(nowBalanceOfATM)")
case .Fail(let failMessage) :
    print(failMessage)
}

/* 结构体与类 */
// 1
struct Coordinate {
    var x : Double
    var y : Double
}

// 2
struct Line {
    var startPoint : Coordinate
    var endPoint : Coordinate
    func length() -> Double {
        return sqrt((startPoint.x - endPoint.x) * (startPoint.x - endPoint.x) +
                    (startPoint.y - endPoint.y) * (startPoint.y - endPoint.y)
        )
    }
}

// 3
let pointA = Coordinate(x: 1, y: 2)
let pointB = Coordinate(x: 3, y: 6)
let lineAB = Line(startPoint: pointA, endPoint: pointB)
print("\(lineAB.length())")

// 4
enum stageOfWashingMachine : String{
    case Ready = "就绪状态"
    case Water = "注水状态"
    case Washing = "洗涤状态"
    case Dewater = "脱水状态"
    case Dry = "烘干状态"
}

class WashingMachine {
    var brand: String
    var isOn: Bool
    var state: stageOfWashingMachine
    func description() {
        if isOn {
            print("Brand: \(brand) State: \(state.rawValue)")
        } else {
            print("The washingmachine is off")
        }
    }
    init(brand: String, isOn: Bool, state: stageOfWashingMachine) {
        self.brand = brand
        self.isOn = isOn
        self.state = state
    }
}

var myWashingMachine = WashingMachine(brand: "Media", isOn: false, state: stageOfWashingMachine.Ready)
myWashingMachine.description()
myWashingMachine.isOn = true
myWashingMachine.description()
myWashingMachine.state = .Washing
myWashingMachine.description()
